﻿package src 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	public class HUD extends MovieClip
	{
		private var num1_rnd:Number;
		private var num2_rnd:Number;
		private var rng_god:Number;
		private var operation_rnd:Number;
		private var number_digits:uint;
		private var range:Number;
		private var numArray:Array;
		private var num_rnd:Number;
		private var num:uint;
		private var duped_num:Boolean;
		
		private var num1:uint;
		private var num2:uint;
		private var option1:uint;
		private var option2:uint;
		private var option3:uint;
		private var option4:uint;
		private var rightOption:uint;
		
		private var par:Bat;
		
		private var _theyArePressed:Object = { };
		
		public var active:Boolean;
		
		public function HUD(myParent:Bat)
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
			par=myParent;
			active=true;
			
		}		
		private function init(e:Event){
			removeEventListener(Event.ADDED_TO_STAGE,init);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed);
			stage.addEventListener(KeyboardEvent.KEY_UP, released);
			
			answer1.letter.text="Q";
			answer2.letter.text="W";
			answer3.letter.text="E";
			answer4.letter.text="R";
			
			createQuestion();
					
		}
		private function createQuestion(){
			
			if (active){
				var rightNum1:Boolean;
				for (var i:uint=0; !rightNum1 ; i++){
					num1_rnd=Math.random();
					num1=Math.round(num1_rnd*9);
					if (num1!=0) {rightNum1=true} else {rightNum1=false}
				}
				
				var rightNum2:Boolean;
				operation_rnd=Math.random();
				if (operation_rnd>=0.75){
	//				operation="suma";
					for (var i:uint=0; !rightNum2 ; i++){
						num2_rnd=Math.random();
						num2=Math.round(num2_rnd*9);
						if (num2!=0) {rightNum2=true} else {rightNum2=false}
						}
					question.text="¿ " + num1.toString() + " + " +num2.toString() + " ?";
					rightOption=num1+num2;
					}
				else if (operation_rnd>=0.5 && operation_rnd<=0.75){
	//				operation="resta";
					for (var i:uint=0; !rightNum2 ; i++){
						num2_rnd=Math.random();
						num2=Math.round(num2_rnd*9);
						if (num2!=0 && num2<=num1) {rightNum2=true} else {rightNum2=false}
						}				
					question.text="¿ " + num1.toString() + " - " +num2.toString() + " ?";
					rightOption=num1-num2;
					}
				else if (operation_rnd>=0.25 && operation_rnd<=0.5){
	//				operation="multiplicacion";
					for (var i:uint=0; !rightNum2 ; i++){
						num2_rnd=Math.random();
						num2=Math.round(num2_rnd*9);
						if (num2!=0) {rightNum2=true} else {rightNum2=false}
						}
					question.text="¿ " + num1.toString() + " x " +num2.toString() + " ?";
					rightOption=num1*num2;
					}
				else if (operation_rnd<=0.25){
	//				operation="division";
					for (var i:uint=0; !rightNum2 ; i++){
						num2_rnd=Math.random();
						num2=num1*Math.round(num2_rnd*9);
						if (num2!=0) {rightNum2=true} else {rightNum2=false}
						}
					question.text="¿ " + num2.toString() + " / " +num1.toString() + " ?";
					rightOption=num2/num1;
					}
				
				if (rightOption/100>1){
					number_digits = 3;
				} else if (rightOption/10>1){
					number_digits = 2;
				} else {number_digits = 1}
				
				numArray= new Array();
				
				for (var i:uint=0; (numArray.length < 3) ; i++){
					num_rnd=Math.random();
					if (number_digits==1){
						num= Math.round(num_rnd*9); 
					}
					else {
						range=2*Math.round(rightOption*0.3);
						num=(rightOption-range/2)+Math.random()*range;
					}
					duped_num=false;
					for (var j:uint=0; j<numArray.length; j++){
						if (numArray[j]==num){
							duped_num= duped_num || true}
						else {
							duped_num= duped_num || false}
					}
					if (num!=0 && num!=rightOption && !duped_num){numArray.push(num)}
				}
				
				rng_god=Math.random();
				if (rng_god>=0.75){
					numArray[3]=numArray[0];
					numArray[0]=rightOption}
				else if (rng_god>=0.5){
					numArray[3]=numArray[1];
					numArray[1]=rightOption}
				else if (rng_god>=0.25){
					numArray[3]=numArray[2];
					numArray[2]=rightOption}
				else {
					numArray[3]=numArray[3]
					numArray[3]=rightOption};
				
				option1 = numArray[0];
				option2 = numArray[1];
				option3 = numArray[2];
				option4 = numArray[3];
				answer1.myText.text = option1.toString();
				answer2.myText.text = option2.toString();
				answer3.myText.text = option3.toString();
				answer4.myText.text = option4.toString();
			}
		}		
		private function pressed(e:KeyboardEvent){
			_theyArePressed[e.keyCode] = true;
			var multiplePressed:Boolean;
			multiplePressed=(_theyArePressed[Keyboard.Q]&&_theyArePressed[Keyboard.W])||(_theyArePressed[Keyboard.Q]&&_theyArePressed[Keyboard.E])
			||(_theyArePressed[Keyboard.Q]&&_theyArePressed[Keyboard.R])||(_theyArePressed[Keyboard.W]&&_theyArePressed[Keyboard.E])
			||(_theyArePressed[Keyboard.W]&&_theyArePressed[Keyboard.R])||(_theyArePressed[Keyboard.E]&&_theyArePressed[Keyboard.R])
			if (!multiplePressed){
				switch( e.keyCode )
				{
					case Keyboard.Q:
						if (option1==rightOption){createQuestion(), par.game.mask_expanding=true, par.game.t=0};
						break;
					case Keyboard.W:
						if (option2==rightOption){createQuestion(), par.game.mask_expanding=true, par.game.t=0};
						break;
					case Keyboard.E:
						if (option3==rightOption){createQuestion(), par.game.mask_expanding=true, par.game.t=0};
						break;
					case Keyboard.R:
						if (option4==rightOption){createQuestion(), par.game.mask_expanding=true, par.game.t=0};
						break;
				}
			}
		}
		
		private function released(e:KeyboardEvent):void {
            _theyArePressed[e.keyCode] = false;
        }
		
	}
}